public class maxDepth {
    /**
     * 二叉树最大深度
     */
    public class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(){}
        TreeNode(int val){
            this.val=val;
        }
        TreeNode(int val, TreeNode left, TreeNode right){
            this.val=val;
            this.left=left;
            this.right=right;
        }
    }

    public int method(TreeNode root){
        if(root==null){
            return 0;
        }
        int leftd = method(root.left);
        int rightd = method(root.right);
        return Math.max(leftd,rightd)+1; // 算上根节点
    }

    public static void main(String[] args) {
        maxDepth m=new maxDepth();

        // 创建二叉树
        TreeNode root=m.new TreeNode(1);
        root.left = m.new TreeNode(1);
        root.right=m.new TreeNode(1);
        root.left.left=m.new TreeNode(1);

        int n= m.method(root);
        System.out.println("n = " + n);
    }
}
